/**
 * @FilePath     : /src/lgmg_robots/agv_navigation/agv_navigate/include/agvWorld/pso.h
 * @Description  : 粒子群优化（PSO）算法类定义，包含粒子配置、优化过程、最佳值和位置获取等功能。 
 * @Author       : xiujun.yang
 * @Version      : 1.0.0
 * @LastEditors  : haibo haibo.yang@lgmgim.cn
 * @LastEditTime : 2024-12-19 15:39:16
 * @Copyright (c) 2024 by 临工智能信息科技有限公司, All Rights Reserved. 
**/
#pragma once
#include "particle.h"
#include <string>
using namespace std;

enum class PSOErrorType {
	PSO_NO_ERROR = 0, 
	PSO_PARTICLE_INVALID = 100,
	PSO_STOP_CRITERIA_INVALID,
};

class PSO
{
 public:
    PSO(int num_particles, int num_variables, double* lower_bounds, double* upper_bounds,
    double* vmax, void (*objective_function)(Particle*), double lower_weight,
    double upper_weight, int max_iterations, double c1, double c2, double threshold,
    const std::string& stopping_criteria, const std::string& pso_type);

    ~PSO();
    // configuration
    void ConfigureParticle(int index, const double* pos, const double* vel);
    int Optimize();
    double GetBestValue() const;
    const double* GetBestPosition() const;
    bool GetBestFeasibility() const;
    void PrintBest() const;
    int GetError() const;
    void SetError(int err);

 private:
    void InitializeParticles();
    void UpdateParticles();
    void UpdateGlobalBest();
    void UpdateConvergence();
    void PrintParticle(int index) const;
    
    double ImpBest() const;
    double ImpAv() const;
    double NoAce() const;
    double MovObj() const;
    double MovPor() const;
    double StdDev() const;
    double MaxDist() const;
    double MaxDistQuick() const;
    double ComCrit() const;
    double DiffMaxDistQuick() const;

    Particle** particles_;
    std::string pso_type_;
    std::string stopping_criteria_;
    int number_of_particles_;
    int best_particle_index_;
    double weight_;
    double lower_weight_;
    double upper_weight_;
    int max_iterations_;
    double threshold_;
    int error_;
    
};

